///*
// * To change this template, choose Tools | Templates
// * and open the template in the editor.
// */
//package com.l4.app.cv;
//
//import com.googlecode.javacpp.Loader;
//import static com.googlecode.javacv.cpp.opencv_core.*;
//import static com.googlecode.javacv.cpp.opencv_highgui.*;
//import static com.googlecode.javacv.cpp.opencv_imgproc.*;
//import com.googlecode.javacv.CanvasFrame;
//import com.googlecode.javacv.FrameGrabber.Exception;
//import com.googlecode.javacv.OpenCVFrameGrabber;
//import com.googlecode.javacv.cpp.opencv_core.*;
//import com.l4.app.main.Timer;
//
///**
// *
// * @author Thusira
// */
//public class FrameAnalyse {
//
//    public static void main(String[] args) throws Exception {
//
//        IplImage backgroundFrame = null, eig_image, temp_image;
//        double[] minVal = new double[1];
//        double[] maxVal = new double[1];
//        CvPoint minLoc = new CvPoint();
//        CvPoint maxLoc = new CvPoint();
////        double minVal = 0, maxVal = 0;
////        CvPoint minLoc = null, maxLoc = null,
//                CvPoint outPoint;
//
////        CvCapture VideoFile = cvCreateFileCapture("D:\\2.avi");
//        boolean DEBUG = true;
////        if (VideoFile == null) {
////            System.out.println("Uh-oh.  Either the input file doesn't exist, or OpenCV cannot read it.");
////            System.exit(0);
////        }
//        OpenCVFrameGrabber grabber = new OpenCVFrameGrabber("D:/2.avi");
//        final CanvasFrame canvas = new CanvasFrame("My Image");
//        final CanvasFrame canvas1 = new CanvasFrame("diff Frame");
//        final CanvasFrame canvas2 = new CanvasFrame("My Image");
//        final CanvasFrame canvas3 = new CanvasFrame("My Image");
//        canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
//        grabber.start();
//        IplImage frame = grabber.grab();
//        CvSize frame_size= cvGetSize(frame);
//
//
//
//
//        // Now let's set up the frame size so that we can vomit out a video...
////        CvSize frame_size =cvGetSize(VideoFile);
////        frame_size.height()=cvGetCaptureProperty(VideoFile, CV_CAP_PROP_FRAME_HEIGHT);
////        frame_size.width((int) cvGetCaptureProperty(VideoFile, CV_CAP_PROP_FRAME_WIDTH));
//        // We'll go ahead and say that the AVI file is loaded now:
//        if (DEBUG) {
//            System.out.println("Brought in AVI file.");
//        }
//
//        // Figure out what our incoming movie file looks like
//        double FPS = (grabber.getFrameRate());
//        String FOURCC = grabber.getFormat();
//        if (DEBUG) {
//            System.out.println("FPS:  " + FPS);
//            System.out.println("FOURCC:  " + FOURCC);
//        }
//
//        // Create a CvVideoWriter.  The arguments are the name of the output file (must be .avi),
//        // a macro for a four-character video codec installed on your system, the desired frame
//        // rate of the video, and the video dimensions.
//        //CvVideoWriter* videoWriter = cvCreateVideoWriter("sidewalk_output.avi",CV_FOURCC('D', 'I', 'V', 'X'), FPS, cvSize(frame_size.width, frame_size.height));
//        // Now we can say that the VideoWriter is created:
//        //if(DEBUG)
//        //{std::cout << "videoWriter is made." << std::endl;}
//
//        // Make display windows
////        cvNamedWindow("background Frame", CV_WINDOW_AUTOSIZE);
////        cvNamedWindow("current Frame", CV_WINDOW_AUTOSIZE);
////        cvNamedWindow("diff Frame", CV_WINDOW_AUTOSIZE);
////        cvNamedWindow("output Frame", CV_WINDOW_AUTOSIZE);
////        cvNamedWindow("ROI Frame", CV_WINDOW_AUTOSIZE);
////        cvNamedWindow("ROI Frame (Color)", CV_WINDOW_AUTOSIZE);
//
//        // Keep track of frames
//
//        int imageCount = 0;
//        // Set up images.
//        IplImage diffFrame = cvCreateImage(cvSize(frame_size.width(), frame_size.height()), IPL_DEPTH_8U, 1);
//        IplImage currentFrame = cvCreateImage(cvSize(frame_size.width(), frame_size.height()), IPL_DEPTH_8U, 1);
//        IplImage outFrame = cvCreateImage(cvSize(frame_size.width(), frame_size.height()), IPL_DEPTH_8U, 3);
//        IplImage tempFrameBGR = cvCreateImage(cvSize(frame_size.width(), frame_size.height()), IPL_DEPTH_8U, 3);
//        IplImage ROIFrame = cvCreateImage(cvSize((265 - 72), (214 - 148)), IPL_DEPTH_8U, 1);
//        IplImage ROIFrame2 = cvCreateImage(cvSize((265 - 72), (214 - 148)), IPL_DEPTH_8U, 1);
//        IplImage ROIFrameBGR = cvCreateImage(cvSize((265 - 72), (214 - 148)), IPL_DEPTH_8U, 3);
//        IplImage ROIFrameBGRPrior = cvCreateImage(cvSize((265 - 72), (214 - 148)), IPL_DEPTH_8U, 3);
//        // And now set up the data for MinMaxLoc (for ROI image)
//
//        // Initialize our contour information...
//        int contours = 0;
//        CvMemStorage storage = cvCreateMemStorage(0);
//        CvSeq firstContour;
//        int headerSize;
//        CvSeq contour = new CvSeq(null);
//        int color = 0;
//        CvContourScanner ContourScanner0;
//
//        // Zero out the people-counting image...
////        cvZero(ROIFrameBGR);
//        int people = 0;
//        int MOVEMENT = 0;
//        // There's gotta be a better way to do this... like with threading?
//        while (true) {
//            // Let's try to threshold this at 15FPS - the input rate.
//            // 66 is used as it's 1/15 * 1000...
//            // Wait a second!  I have the FPS here.  *sigh*  Let's do this dynamically:
//            //Sleep((1000/FPS)-10);
//            // Awesome.  The video runs in actual time now, after subtracting out the 10ms from WaitKey().
//
//            IplImage tempFrame = grabber.grab();
//            // If the video HAS a current frame...
//            if (tempFrame != null) {
//                // The video is BGR-space.  I wish there were a cvGetColorSpace command or something...
//                cvCvtColor(tempFrame, currentFrame, CV_BGR2GRAY);
//
//                //cvFlip(currentFrame);
//                // Get initial "background" image...
//                if (imageCount == 0) {
//                    //IplImage* backgroundFrame = cvCloneImage(currentFrame);
//                    backgroundFrame = cvCloneImage(currentFrame);
//                }
////                cvShowImage("background Frame", backgroundFrame);
////                cvShowImage("current Frame", tempFrame);
//                canvas.showImage(tempFrame);
//                cvAbsDiff(currentFrame, backgroundFrame, diffFrame);
//                if (DEBUG) {
//                    System.out.println("Pulled in video grab of frame " + imageCount);
//                }
//
//                // Back to color ...
//                cvCvtColor(diffFrame, outFrame, CV_GRAY2BGR);
//
//                // Now let's go ahead and put up a box (rect, actually) for our ROI.
//                // (72, 148)+-----------------------+(265, 148)
//                //			|						|
//                // (72, 214)+-----------------------+(265, 214)
//                //MotionRegion cvRect(72, 148, (265-72), (214-148));
//                //cvRectangle(outFrame, cvPoint(72, 148), cvPoint(265, 214), CV_RGB(255, 0, 255), 1);
//                cvRectangle(diffFrame, cvPoint(72, 148), cvPoint(265, 300), cvScalar(255, 0, 255,0),1, 8, 0);
//                canvas1.showImage(diffFrame);
//                cvCvtColor(backgroundFrame, tempFrameBGR, CV_GRAY2BGR);
//                //cvFlip(tempFrame);
//                // ROIFrame is BW.
//                ROIFrame = cvCloneImage(outFrame);
//                cvSetImageROI(ROIFrame, cvRect(72, 148, (265 - 72), (300 - 148)));
//                //cvOr(outFrame, tempFrame, outFrame);
//                canvas1.showImage(ROIFrame);
//
//
//                cvSetImageCOI(ROIFrame, 1);
//                cvMinMaxLoc(ROIFrame, minVal,maxVal,minLoc,maxLoc, null);
////                if (maxVal < 100) {
////                    // Zero out the LAST people-counting image...
////                    cvZero(ROIFrameBGRPrior);
////                    MOVEMENT = 0;
////                }
////                if (maxVal > 100) {
////                    cvSetImageCOI(ROIFrameBGRPrior, 1);
////                    // We are starting a motion sequence...
////                    if ((MOVEMENT == 0) && (cvCountNonZero(ROIFrameBGRPrior) == 0)) {
////                        // Zero out the people-counting image...
////                        cvZero(ROIFrameBGR);
////                        MOVEMENT = 1;
////                        people++;
////                        if (DEBUG) {
////                            std::
////                            cout << "ROI has counted " << people << " people." << std:
////                            :endl;
////                        }
////                    }
////
////                    if (DEBUG) {
////                        std::
////                        cout << "We have motion in the ROI!  maxVal: " << maxVal << " minVal: " << minVal << std:
////                        :endl;
////                    }
////                    // Phew.  Okay, we can figure out when there's motion within the ROI.  Good.
////                    // Now let's see what we can do with contours.
////                    //contours = cvFindContours(ROIFrame, storage, firstContour, headerSize=sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
////                    //contours = cvFindContours(ROIFrame, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
////                    // Bah.  Couldn't do anything with them.  :(
////                    // Let's instead try to put a dot on people that are moving...
////                    //cvCircle( CvArr* img, CvPoint center, int radius, double color, int thickness=1 )
////                    cvCircle(ROIFrameBGR, maxLoc, 1, CV_RGB(255, 0, 0), 1);
////                    ROIFrameBGRPrior = cvCloneImage(ROIFrameBGR);
////                }
////                cvShowImage("ROI Frame (Color)", ROIFrameBGR);
////                /*
////                // Now:  a better way - we'll know there's motion if contours>0.
////                ROIFrameBGR = cvCloneImage(ROIFrame);
////                cvCvtColor(ROIFrame, ROIFrameBGR, CV_GRAY2BGR);
////                cvSetImageCOI(ROIFrame, 1);
////
////                contours = cvFindContours(ROIFrame, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
////                if (contours > 0)
////                {
////                if(DEBUG)
////                {std::cout << "We have motion in the ROI!" << std::endl;}
////                }
////
////                // Draw out the contours
////                for( ; contour != 0; contour = contour->h_next )
////                {
////                // replace CV_FILLED with 1 to see the outlines
////                cvDrawContours( ROIFrameBGR, contour, CV_RGB( rand(), rand(), rand() ), CV_RGB( rand(), rand(), rand() ), -1, CV_FILLED, 8 );
////                }
////                cvShowImage("ROI Frame (Color)", ROIFrameBGR);
////                 */
////
////                // Write the current frame to an output movie.
////                //cvWriteFrame(videoWriter, diffFrame);
////                // Build up the output ...
////                cvOr(outFrame, tempFrame, outFrame);
////                // ... and draw the ROI rectangle.
////                cvRectangle(outFrame, cvPoint(72, 148), cvPoint(265, 300), CV_RGB(255, 0, 255), 1);
////                char peopleCount[
////
////
////
////                   32];
////			if (people==1) {
////                    sprintf(peopleCount, "%d person", people);
////                } else if ((people < 1) || (people > 1)) {
////                    sprintf(peopleCount, "%d people", people);
////                }
////                CvFont font;
////                cvInitFont(&  font, CV_FONT_HERSHEY_SIMPLEX, 0.8, 0.8, 0, 2);
////                cvPutText(outFrame, peopleCount, cvPoint(0, 25), &  font, cvScalar(0, 0, 300));
////                cvShowImage("output Frame", outFrame);
////                //cvWriteFrame(videoWriter, outFrame);
////                if (DEBUG) {
////                    std::
////                    cout << "Wrote frame to output AVI file." << std:
////                    :endl;
////                }
////                imageCount++;
////            }	// end if (image != NULL) loop
////
////            // This will return the code of the pressed key or -1 if
////            // nothing was pressed before 10 ms elapsed.
//////            int keyCode = cvWaitKey(10);
//////            if ((keyCode == 's') || (keyCode == 'S')) {
//////                while (1) {
//////                    keyCode = cvWaitKey(10);
//////                    if ((keyCode == 's') || (keyCode == 'S')) {
//////                        keyCode = 999;
//////                        break;
//////                    }
//////                }
//////            }
//
//        }// end while loop
//
//    }
//    }
//}
//
